package com.voxel.sense.infomsg.view;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import com.example.com.common.utils.ALDisplayMetricsManager;
import com.example.com.common.utils.ScreenUtils;


/**
 * Created by chenyiyao on 2017/10/27.
 */

public class TransStatusBottomSheetDialog extends BottomSheetDialog {
    private BottomSheetBehavior behavior;

    public TransStatusBottomSheetDialog(@NonNull Context context) {
        super(context);
    }

    public TransStatusBottomSheetDialog(@NonNull Context context, int theme) {
        super(context, theme);
    }

    protected TransStatusBottomSheetDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Window window = getWindow();
        if (window == null)
            return;


        // 得到屏幕高度
        int screenHeight = ScreenUtils.getScreenHeight();
        // 得到状态栏的高度
        int statusHeight = ALDisplayMetricsManager.getStatusBarHeight(getOwnerActivity());

        // 计算dialog的高度并设置
        int dialogHeight = screenHeight - statusHeight;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                dialogHeight <= 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
    }


    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        initBehavior();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        initBehavior();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResId) {
        super.setContentView(layoutResId);
        initBehavior();
    }

    private void initBehavior() {
        FrameLayout bottomSheet = (FrameLayout) findViewById(android.support.design.R.id.design_bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != behavior && (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN)) {
            behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    }

}
